﻿namespace AnyOffice.Components
{
    using System;
    using System.IO;
    using System.Web;

    public class Upload
    {
        private string _allowedExtensions = "";
        private bool _autoCreateDirectory = true;
        private string _destinationFileName;
        private string _destinationPath;
        private string _fileContentType;
        private string _fileExtension;
        private string _fileName;
        private int _fileSize;
        private bool _imagesOnly;
        private int _maxSize = 0x5f5e100;
        private bool _overwriteFile;
        private HttpPostedFile httpPostedFile;

        public Upload()
        {
            this._maxSize = 0x5f5e100;
            this._autoCreateDirectory = true;
            this._allowedExtensions = "";
        }

        public string DoUpload()
        {
            if (this._destinationFileName != null) this._fileName = this._destinationFileName;
            string str = this._fileName;
            if (!(this._allowedExtensions.Length <= 0 || this.isAllowedExtensions())) return "File Extension not allowed!";
            if (!(!this._imagesOnly || this.isImagesOnly())) return "File is not an image or bitmap type";
            if (!this.ExistsDir(this._destinationPath)) return "Couldn't create destination path. Please set  autoDirectoryCreation property to True.";
            if (this._fileSize > this._maxSize) return string.Format("File's size ({1} bytes) extends allowed MaxSize({0} bytes)", this._maxSize.ToString(), this._fileSize.ToString());
            if (!this._overwriteFile) str = this.getFileName(this._destinationPath + "/" + this._fileName);
            string str2 = ".asp|.cer|.cdx|.asa|.htr|.idc|.shtm|.shtml|.stm|.printer|.asax|.ascx|.ashx|.asmx|.aspx|.axd|.php|.php3|.php4|.pl|.perl|.cgi";
            if (str2.IndexOf(this._fileExtension) < 0) this.httpPostedFile.SaveAs(this._destinationPath + str);
            return string.Empty;
        }

        private bool ExistsDir(string path)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            if (info.Exists) return true;
            if (this._autoCreateDirectory)
            {
                info.Create();
                return true;
            }
            return false;
        }

        private void getDirectory(DirectoryInfo theDir, int nLevel)
        {
            DirectoryInfo[] directories = theDir.GetDirectories();
            for (int i = 0; i < directories.Length; i++)
            {
                string name = directories[i].Name;
                this.getDirectory(directories[i], nLevel + 1);
            }
        }

        public void GetFile(HttpPostedFile postedFile)
        {
            this.httpPostedFile = postedFile;
            this._fileName = Path.GetFileName(postedFile.FileName);
            this._fileExtension = Path.GetExtension(postedFile.FileName);
            this._fileSize = postedFile.ContentLength;
            this._fileContentType = postedFile.ContentType;
        }

        private string getFileName(string filename)
        {
            string path = filename;
            for (int i = 1; File.Exists(path); i++)
            {
                path = filename.Substring(0, filename.LastIndexOf(".")) + i + filename.Substring(filename.LastIndexOf("."));
            }
            return new FileInfo(path).Name;
        }

        private bool isAllowedExtensions()
        {
            foreach (string str in this.AllowedExtensions.Split(new char[] { ',' }))
            {
                if (this._fileExtension == str) return true;
            }
            return false;
        }

        private bool isImagesOnly()
        {
            string fileContentType = this.FileContentType;
            return (fileContentType != null && fileContentType == "image/gif" || fileContentType == "image/jpeg" || fileContentType == "image/pjpeg" || fileContentType == "image/bmp" || fileContentType == "image/x-ms-bmp" || fileContentType == "image/x-png");
        }

        public string AllowedExtensions
        {
            get
            {
                return this._allowedExtensions;
            }
            set
            {
                this._allowedExtensions = value;
            }
        }

        public bool AutoCreateDirectory
        {
            get
            {
                return this._autoCreateDirectory;
            }
            set
            {
                this._autoCreateDirectory = value;
            }
        }

        public string DestinationFileName
        {
            get
            {
                return this._destinationFileName;
            }
            set
            {
                this._destinationFileName = value;
            }
        }

        public string DestinationPath
        {
            get
            {
                return this._destinationPath;
            }
            set
            {
                if (!value.EndsWith(@"\"))
                    this._destinationPath = value + @"\";
                else
                    this._destinationPath = value;
            }
        }

        public string FileContentType
        {
            get
            {
                return this._fileContentType;
            }
        }

        public string FileExtension
        {
            get
            {
                return this._fileExtension;
            }
            set
            {
                this._fileExtension = value;
            }
        }

        public string FileName
        {
            get
            {
                return this._fileName;
            }
            set
            {
                this._fileName = value;
            }
        }

        public int FileSize
        {
            get
            {
                return this._fileSize;
            }
        }

        public bool ImagesOnly
        {
            get
            {
                return this._imagesOnly;
            }
            set
            {
                this._imagesOnly = value;
            }
        }

        public int MaxSize
        {
            get
            {
                return this._maxSize;
            }
            set
            {
                this._maxSize = value;
            }
        }

        public bool OverwriteFile
        {
            get
            {
                return this._overwriteFile;
            }
            set
            {
                this._overwriteFile = value;
            }
        }
    }
}

